<?
include 'includes/specs_functions.php';
#----------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;
$THEME = $SESSION->Theme;
#---------------------------------------------------
$specId = $_POST['spec'];
$ver = "2.02"; // Make static for live!
if($THEME=="light"){
	$color = "#FFFFFF";
}else{
	$color = "#222222";
}
if($specId != "" && $specId != undefined){
	$table = "((SELECT * FROM ".SPEC_TBL_SPECS." WHERE ".SPEC_TBL_SPECS_ID." = \"".$specId."\") spec
		LEFT JOIN
		".TBL_CHARACTERS." user
		ON spec.".SPEC_TBL_SPECS_USER_ID." = user.".TBL_CHARACTERS_CICUID.")";
	$col = array("spec.".SPEC_TBL_SPECS_LAYOUT." as layout"
				, "spec.".SPEC_TBL_SPECS_USER_ID." as userId"
				, "user.".TBL_CHARACTERS_NAME." as Name"
				, "user.".TBL_CHARACTERS_AVATAR." as Avatar"
				, "spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
				, "spec.".SPEC_TBL_SPECS_PUBLIC." as public"
				);
	$where = array();
	$orderBy = "spec.".SPEC_TBL_SPECS_USER_ID;
	$dir = "ASC";
	
	$result = $DB->GetData($table ,$col ,true ,null, $orderBy, $dir, null);
	if ($result[0] == 1){
		foreach ($result[3] as $row) {
			$temp_pub = $row['public'];
			$temp_uid = $row['userId'];
			if($temp_pub == "Y" || $temp_uid == $CICUID){
				$layout = $row["layout"];
				$specCICUID = $row["userId"];
				$desc = $row["description"];
				$userName = $row["Name"];
				$userAvatar = $row["Avatar"];
			}else{
				$specId = -1;
			}
		}
	}else{
		$specId = -1;
	}
}else{
	$specId = -1;
}
$sowner = 0;
$canRate = false;

if( $specId != -1 ){
	/* IS LOGGD IN */
	if($specCICUID==$CICUID){
		/* IS OWNER DONT LET HIM RATE */
		$sowner = 1;
	}else if($CICUID != ""){
		$canRate = true;
		/* GET HIS OLD RATING IF THER IS */
		
		$table = SPEC_TBL_RATINGS;
		$col = array(SPEC_TBL_RATINGS_VALUE." as value");
		$where = array(SPEC_TBL_RATINGS_USER_ID."= \"".$CICUID."\"", 
						SPEC_TBL_RATINGS_SPEC_ID ."= \"".$specId."\"");
		$orderBy = SPEC_TBL_RATINGS_VALUE;
		$dir = "ASC";
		$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $dir, null);
		
		foreach ($result[3] as $row) {
			$rateValue = $row['value'];
		}
		
	}
}
?>
<?PHP
	if($specId != -1){
		echo "<script>loadInfoSpecs(".$specId.")</script>"; 
	}
?>


		<script type="text/javascript" src="../scripts/tooltip/tooltip.js"></script>
		<script>		
					$(document).ready(function(){
						$('.show-user').click(function(e) {	
							var clicked = jQuery(e.target);
							var id = clicked.attr('id');
							getUser(id);
						});
						
						$('.spec_rate').stars({
							cancelShow: false
							,callback: function(ui, type, value){
								$('#rating_status').html('<div class=\"center loader-gif\" style=\"margin-top:2px;\"></div>');
								$.post('./specs/php/saveRating.php', addSessionVars({specId: <? echo $specId; ?>, val: value}),
									function(data){
										if(data.status == 'success'){
											$('#rating_status').html("Rating saved");
										}else{
											$('#rating_status').html("Please try again");
										}
									   }, 'json');
								
							}
							
						
						});
						
					});
		</script>
		<div id='indent-content-container' class='me-content-container'>
			<!--<div class = 'content-subtitle-container-open'></div>-->
			
<?
			if($canRate){
				echo	"<div style=\"margin-bottom: 20px;margin-top: 10px;\">
					<div>";
					echo "<div id='rate_text' class='normal-text fl-left' style='padding-right: 10px;padding-left: 2px;margin-left: 19px;'>Rate ";
					echo "<a  href='?".$specCICUID."' class='with-tooltip blue-text cur-point' title=\"<img src='".str_replace(" ", "", $userAvatar)."'></img>\" > ".$userName."</a>";
					echo " 's spec: </div>";
					echo "
						<div class='fl-left'>
							<form class='spec_rate' action='' method='post' style='padding-top: 1px;'>";
								 foreach (get_options() as $id => $title){
									echo "<input type='radio' name='rate' value='".$id."' id='rate".$id."'";
									if($id==$rateValue){
										echo " checked";
									}
									echo " />";
								}
					echo	"</form>
						</div>
						<div id='rating_status' class='fl-left normal-text blue-text' style='margin-top: 0px;margin-left: 26px;'></div>
					</div>";

			}else{
				echo "<div style=\"margin-bottom: 20px;z-index:0;\">";
			}
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);
?>
				<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
					codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,16,0"
					width="735" height="720" >
					<param name="movie" value="specs/swf/ffxivcalc.swf?theme=<? echo(urlencode($THEME)) ?>&layout=<? echo(urlencode($layout)) ?>&userId=<? echo(urlencode($CICUID)) ?>&specId=<? echo(urlencode($specId)) ?>&sowner=<? echo($sowner) ?>&desc=<? echo(urlencode($desc)) ?>&version=<? echo $ver; ?>">
					<param name="quality" value="high">
					<param name="play" value="true">
					<param name="LOOP" value="false">
					<param name="bgcolor" value="<? echo $color; ?>">
					<embed src="specs/swf/ffxivcalc.swf?theme=<? echo(urlencode($THEME)) ?>&layout=<? echo(urlencode($layout)) ?>&userId=<? echo(urlencode($CICUID)) ?>&specId=<? echo(urlencode($specId)) ?>&sowner=<? echo($sowner) ?>&desc=<? echo(urlencode($desc)) ?>&version=<? echo $ver; ?>" bgcolor="<? echo $color; ?>" width="735" height="720" play="true" loop="false" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" 
					type="application/x-shockwave-flash">
					</embed>
				</object>
			</div>
			<!--<div class = 'content-subtitle-container-close'></div>-->
		</div>